package com.hulk.rpc.discover.local;

import com.hulk.rpc.annotation.HulkService;
import com.hulk.rpc.config.common.HostPort;
import com.hulk.rpc.config.server.ProtocolType;
import com.hulk.rpc.discover.Discover;
import com.hulk.rpc.discover.DiscoverListener;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xuxukang
 * @date 2019-04-17 22:33
 */
@Slf4j
public class DirectDiscover implements Discover {

  private List<HostPort> hostPorts;

  @Override
  public void init(List<HostPort> hostPorts) {
    this.hostPorts = hostPorts;
  }

  @Override
  public void startRegistryListener(String group, String app, ProtocolType protocolType,
                                    DiscoverListener listener) {
    Map<HostPort, Integer> hostPortWeightMap = hostPorts.stream()
            .collect(Collectors.toMap(item -> item, item -> HulkService.default_weight));

    // init后直接调用匿名函数
    listener.onChange(hostPortWeightMap);
  }

  @Override
  public void close() throws IOException {
    log.info("【info】{}", "DirectDiscover.close");
  }
}
